<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" size="mini" @click="deleteAll"
          >批量删除</el-button
        >
        &nbsp;
        <el-input
          v-model="select_word"
          style="width: 230px"
          size="mini"
          placeholder="请输入用户名"
          class="handle-input"
        ></el-input>
        &nbsp;
        <el-input
          v-model="select_word1"
          style="width: 230px"
          size="mini"
          placeholder="请输入博客标题"
          class="handle-input"
        ></el-input>
        &nbsp;
        <el-input
          v-model="select_word2"
          style="width: 230px"
          size="mini"
          placeholder="请输入博客类型"
          class="handle-input"
        ></el-input>
        &nbsp;
        <el-input
          v-model="select_word3"
          style="width: 230px"
          size="mini"
          placeholder="请输入博客状态"
          class="handle-input"
        ></el-input>
        &nbsp;
        <el-input
          v-model="select_word4"
          style="width: 230px"
          size="mini"
          placeholder="请输入博客分类"
          class="handle-input"
        ></el-input>
        &nbsp;
        <el-input
          v-model="select_word5"
          style="width: 230px"
          size="mini"
          placeholder="请输入博客标签"
          class="handle-input"
        ></el-input>
        &nbsp; &nbsp;
        <el-button type="primary" size="mini" @click="getData"
          >查询博客</el-button
        >
      </div>
    </div>
    <el-table
      size="mini"
      ref="multipleTable"
      border
      style="width: 100%"
      height="680px"
      :data="tableData"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="40"></el-table-column>
      <el-table-column label="博客图片" width="110" align="center">
        <template slot-scope="scope">
          <div class="consumer-img">
            <img
              :src="'http://localhost:8081/blog' + scope.row.img"
              style="width: 100%"
            />
          </div>
          <el-upload
            :headers="headers"
            :action="uploadUrl(scope.row.id)"
            :show-file-list="false"
            :before-upload="beforeAvatorUpload"
            :on-success="handleAvatorSuccess"
          >
            <el-button size="mini">更新图片</el-button>
          </el-upload>
        </template>
      </el-table-column>
      <el-table-column
        prop="username"
        label="用户名"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="title"
        label="标题"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="content"
        label="内容"
        width="297"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="type"
        width="100"
        label="类型"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="state"
        label="状态"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="views"
        label="观看量"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="type_name"
        label="分类"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="tags"
        label="标签"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="score"
        label="分数"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="create_time"
        label="创建时间"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column label="评论管理" width="80" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="getComment(tableData[scope.$index].id)"
            >评论</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="deleteRow(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="total,prev,pager,next"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="total"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      tempData: [],
      select_word: "",
      select_word1: "",
      select_word2: "",
      select_word3: "",
      select_word4: "",
      select_word5: "",
      pageSize: 5, //分页每页大小
      currentPage: 1, //当前页
      total: 0,
      idx: -1, //当前选择项
      multipleSelection: [], //哪些项已经打勾
    };
  },
  created() {
    this.getData();
  },
  computed: {
    headers() {
      return { Authorization: sessionStorage.getItem("tokenStr") };
    },
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //获取当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData();
    },
    //查询所有用户
    getData() {
      this.tempData = [];
      this.tableData = [];
      let param =
        "/list/" +
        (this.currentPage - 1) +
        "/" +
        this.pageSize +
        "?username=" +
        this.select_word +
        "&title=" +
        this.select_word1 +
        "&type=" +
        this.select_word2 +
        "&state=" +
        this.select_word3 +
        "&typeName=" +
        this.select_word4 +
        "&tagName=" +
        this.select_word5;
      this.$get(param).then((res) => {
        if (res) {
          this.tableData = res.data.blog;
          this.tempData = res.data.blog;
          this.total = res.data.total;
        }
      });
    },
    //更新图片
    uploadUrl(id) {
      return `http://localhost:8081/blog/pic/${id}`;
    },
    //删除一名用户
    deleteRow(val) {
      this.$confirm("您确定要删除吗？", "提示", {
        type: "warning",
      }).then(() => {
        this.$delete("delete/" + val).then((res) => {
          if (res.code == 200) {
            this.getData();
          }
        });
      });
    },
    deleteAll() {
      this.$confirm("您确定要删除吗？", "提示", {
        type: "warning",
      }).then(() => {
        for (var i = 0; i < this.multipleSelection.length; i++) {
          this.$delete("delete/" + this.multipleSelection[i].id).then((res) => {
            if (res.code == 200) {
              this.getData();
            } else {
              this.$message.error(res.message);
            }
          });
        }
      });
    },
    beforeAvatorUpload(file) {
      var testMsg = file.name.substring(file.name.lastIndexOf(".") + 1);
      if (testMsg != "jpg" && testMsg != "jpeg" && testMsg != "png") {
        this.$message({
          message: "上传文件只能是jpg、jpeg、png格式",
          type: "error",
        });
        return false;
      }
      return true;
    },
    handleAvatorSuccess(res) {
      if (res.code == 200) {
        this.$message.success(res.message);
      } else {
        this.$message.error(res.message);
      }
    },
    getComment(id) {
      this.$router.push({ path: "/home/comment", query: { id } });
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
  float: left;
}
.consumer-img {
  width: 100%;
  height: 80px;
  border-radius: 5px;
  margin-bottom: 5px;
  overflow: hidden;
}
.handle-input {
  width: 250px;
  display: inline-block;
}
.pagination {
  display: flex;
  justify-content: center;
}
</style>